package com.agriculture.mapper;

import com.agriculture.domain.Inventory;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface InventoryMapper {
    @Insert("INSERT INTO inventories(product_id, owner_id, current_stock, minimum_stock, operator, created_at, update_time) " +
            "VALUES(#{product_id}, #{owner_id}, #{current_stock}, #{minimum_stock}, #{operator}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "inventory_id")
    int insert(Inventory inventory);

    @Select("SELECT * FROM inventories WHERE product_id = #{productId} AND owner_id = #{ownerId}")
    Inventory findByProductId(@Param("productId") Long productId, @Param("ownerId") Long ownerId);

    @Update("UPDATE inventories SET current_stock = #{currentStock}, update_time = NOW() " +
            "WHERE product_id = #{productId} AND owner_id = #{ownerId}")
    int updateStock(@Param("productId") Long productId, 
                   @Param("currentStock") Integer currentStock,
                   @Param("ownerId") Long ownerId);

    @Select("SELECT * FROM inventories WHERE current_stock <= minimum_stock AND owner_id = #{ownerId}")
    List<Inventory> findLowStock(@Param("ownerId") Long ownerId);

    List<Inventory> getStockList(@Param("ownerId") Long ownerId, 
                                @Param("search") String search, 
                                @Param("offset") int offset, 
                                @Param("pageSize") int pageSize);

    int getStockCount(
            @Param("ownerId") Long ownerId,
            @Param("search") String search);
} 